package com.spring.shops.service.impl;

import com.spring.shops.dao.*;
import com.spring.shops.model.entity.TzArea;
import com.spring.shops.model.entity.TzTransfee;
import com.spring.shops.model.entity.TzTransfeeFree;
import com.spring.shops.model.entity.TzTransport;
import com.spring.shops.model.result.ServerResponseEntityVoid;
import com.spring.shops.model.vo.*;
import com.spring.shops.service.TransportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class TransportServiceImpl implements TransportService {

    @Resource
    private TzTransportMapper tzTransportMapper;
    @Autowired
    private TzTransportMapper mapper;
    @Autowired
    private TzTransfeeMapper feeMapper;
    @Autowired
    private TzTransfeeFreeMapper feeFreeMapper;
    @Autowired
    private TzTranscityFreeMapper cityFreeMapper;
    @Autowired
    private TzTranscityMapper cityMapper;
    @Autowired
    private TzAreaMapper areaMapper;

    @Override
    public TransportVo selectTransportsPage(int currentPage, int pageSize ,String transName) {
        int current=(currentPage-1)*pageSize;
        List<TzTransport> records1 ;
        int total;
        if ("1".equals(transName)){
            records1 = tzTransportMapper.selectTransportPage(current,pageSize);
            total=tzTransportMapper.selectCount();
        }else {
            records1 = tzTransportMapper.selectByLike(transName);
            total=records1.size();
        }

        int pages;
        if (total%pageSize==0){
            pages=total/pageSize;
        }else {
            pages=(total/pageSize)+1;
        }


        TzTransfeeFree [] transfeeFrees =new TzTransfeeFree[0];
        TzTransfee [] Transfees =new TzTransfee[1];
        TzArea [] tzAreas =new TzArea[1];
        TzTransfee transfee =new TzTransfee();
        transfee.setTransfeeId((long)115);
        transfee.setTransportId((long)47);
        BigDecimal bc = new BigDecimal("1.0");
        transfee.setContinuousPiece(bc);
        transfee.setFirstPiece(bc);
        transfee.setContinuousFee(bc);
        transfee.setFirstFee(bc);
        Transfees[0]=transfee;
        TzArea tzArea =new TzArea();
        tzArea.setAreaId((long) 1);
        tzArea.setAreaName("北京");
        tzArea.setAreas("中山区");
        tzArea.setLevel(1);
        tzArea.setParentId((long) 1);
        tzAreas[0]=tzArea;
        transfee.setCityList(tzAreas);

        List<TzTransport> records =new ArrayList<>();
        for (TzTransport t:records1){
            TzTransport transport = new TzTransport();
            transport.setTransportId(t.getTransportId());

            transport.setTransfees(Transfees);
            transport.setTransfeeFrees(transfeeFrees);

            transport.setIsFreeFee(t.getIsFreeFee());
            transport.setChargeType(t.getChargeType());
            transport.setHasFreeCondition(t.getHasFreeCondition());
            transport.setTransName(t.getTransName());
            transport.setCreateTime(t.getCreateTime());
            transport.setShopId(t.getShopId());
            records.add(transport);
        }
        TransportVo transportVo =new TransportVo();
        transportVo.setRecords(records);
        transportVo.setCurrent(currentPage);
        transportVo.setPages(pages);
        transportVo.setSize(pageSize);
        transportVo.setTotal(total);
        return transportVo;
    }

    @Override
    public int deletedByTzTransportId(Long transportId) {
        int i = tzTransportMapper.deleteByPrimaryKey(transportId);
        return i;
    }

    @Override
    public int insertTzTransport(TzTransport record) {
        int i = tzTransportMapper.insertSelective(record);
        return i;
    }

    @Override
    public int updateTzTransport(TzTransport tzTransport) {
        int i = tzTransportMapper.updateByPrimaryKeySelective(tzTransport);
        return i;
    }

    @Override
    public TzTransport selectById(Long id) {
        return tzTransportMapper.selectByPrimaryKey(id);
    }

    @Override
    public ServerResponseEntityVoid<List<TransportVO2>> list() {
        List<TzTransport> list=tzTransportMapper.listAllTransport();
        List<TransportVO2> res=new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //属性赋值
        for (TzTransport transport : list) {
            res.add(new TransportVO2(
                    transport.getTransportId(),transport.getTransName(), sdf.format(transport.getCreateTime()),
                    transport.getShopId(), Integer.valueOf(transport.getChargeType()),
                    Integer.valueOf(transport.getIsFreeFee()),Integer.valueOf(transport.getHasFreeCondition()),
                    null,null
            ));
        }
        return ServerResponseEntityVoid.success(res);
    }
    @Override
    public ServerResponseEntityVoid<TransportVO2> getInfoById(Long id) {
        //根据id获取运费模板id
        TzTransport transport = mapper.selectByPrimaryKey(id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //属性赋值
        TransportVO2 transportVO = new TransportVO2(
                transport.getTransportId(), transport.getTransName(), sdf.format(transport.getCreateTime()),
                transport.getShopId(), Integer.valueOf(transport.getChargeType()),
                Integer.valueOf(transport.getIsFreeFee()), Integer.valueOf(transport.getHasFreeCondition()),
                null, null
        );
        //设置transportVO的transfeeFrees和transfees;
        //根据运费模板id获取所有的运费项
        List<TzTransfee> feeList=feeMapper.selectByTransId(id);
        List<TransFeeVO> feeListVO=new ArrayList<>();
        //属性拷贝
        for (TzTransfee fee : feeList) {
            //获取运费项id
            Long feeId = fee.getTransfeeId();
            //根据运费项id查询免免费城市id
            List<Long> cityIds=cityMapper.getCityIdByFeeId(feeId);
            //根据城市Ids查询所有的地区
            List<TzArea> areaList=new ArrayList<>();
            if(cityIds!=null&& !cityIds.isEmpty()){
                for (Long cityId : cityIds) {
                    TzArea tzArea = areaMapper.selectById(cityId);
                    areaList.add(tzArea);
                }
            }
            //属性拷贝
            List<AreaVO> areaListVO=new ArrayList<>();
            if(areaList!=null&&!areaList.isEmpty()){
                for (TzArea area : areaList) {
                    areaListVO.add(new AreaVO(
                            area.getAreaId(), area.getAreaName(), area.getParentId(),
                            area.getLevel(),null
                    ));
                }
            }

            feeListVO.add(new TransFeeVO(
              fee.getTransfeeId(),fee.getTransportId(),
                   fee.getContinuousPiece().doubleValue(),fee.getFirstPiece().doubleValue(),
                  fee.getContinuousFee().doubleValue(),fee.getFirstFee().doubleValue(),areaListVO
           ));
        }
        transportVO.setTransfees(feeListVO);
            //根据运费模板id获取所有的免运费项
            List<TzTransfeeFree> feeFreeList=feeFreeMapper.selectByTransId(id);
            List<TransFeeFreeVO> feeFreeListVO=new ArrayList<>();
            //属性拷贝
            for (TzTransfeeFree free : feeFreeList) {
                //获取免运费项id
                Long transfeeFreeId = free.getTransfeeFreeId();
                //根据免运费项id查询免运免费城市id
                List<Long> cityIds=cityFreeMapper.getCityIdByFreeId(transfeeFreeId);
                //根据城市Ids查询所有的地区
                List<TzArea> areaList=new ArrayList<>();
                if(cityIds!=null&& !cityIds.isEmpty()){
                    for (Long cityId : cityIds) {
                        areaList.add(areaMapper.selectById(cityId));
                    }
                }

                //属性拷贝
                List<AreaVO> areaListVO=new ArrayList<>();
                if(areaList!=null&&!areaList.isEmpty()){
                    for (TzArea area : areaList) {
                        areaListVO.add(new AreaVO(
                                area.getAreaId(), area.getAreaName(),area.getParentId(),
                                area.getLevel(),null
                        ));
                    }
                }
                feeFreeListVO.add(new TransFeeFreeVO(
                        free.getTransfeeFreeId(),free.getTransportId(),
                        Integer.valueOf(free.getFreeType()),
                        free.getAmount().doubleValue(),free.getPiece().doubleValue(),
                        areaListVO
                ));
            }
            transportVO.setTransfeeFrees(feeFreeListVO);
        return ServerResponseEntityVoid.success(transportVO);
    }
}


//package com.spring.shops.service.impl;
//
//import com.spring.shops.dao.*;
//import com.spring.shops.model.entity.TzArea;
//import com.spring.shops.model.entity.TzTransfee;
//import com.spring.shops.model.entity.TzTransfeeFree;
//import com.spring.shops.model.entity.TzTransport;
//import com.spring.shops.model.result.ServerResponseEntityVoid;
//import com.spring.shops.model.vo.AreaVO;
//import com.spring.shops.model.vo.TransFeeFreeVO;
//import com.spring.shops.model.vo.TransFeeVO;
//import com.spring.shops.model.vo.TransportVO;
//import com.spring.shops.service.TransportService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.List;
//
//@Service
//public class TransportServiceImpl implements TransportService {

//    /**
//     * 获取所有的运费模板
//     */
//    @Override
//    public ServerResponseEntityVoid<List<TransportVO>> list() {
//        List<TzTransport> list=mapper.listAllTransport();
//        List<TransportVO> res=new ArrayList<>();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        //属性赋值
//        for (TzTransport transport : list) {
//            res.add(new TransportVO(
//                    transport.getTransportId(),transport.getTransName(), sdf.format(transport.getCreateTime()),
//                    transport.getShopId(), Integer.valueOf(transport.getChargeType()),
//                    Integer.valueOf(transport.getIsFreeFee()),Integer.valueOf(transport.getHasFreeCondition()),
//                    null,null
//            ));
//        }
//        return ServerResponseEntityVoid.success(res);
//    }
//    /**
//     * 根据运费模板id获取具体信息
//     * @param id
//     * @return
//     */

//}